package com.fwj.common.config;

import com.fwj.common.interceptor.WebSocketInterceptor;
import com.fwj.common.handle.ChatWebSocketHandle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

/**
 * @Author: fwj
 * @Data: 2022/5/3 13:35
 * @Version 1.0
 * @Description:    WebSocket配置文件
 */

@Configuration
public class WebSocketConfig implements WebSocketConfigurer {

    @Autowired
    private ChatWebSocketHandle chatWebSocketHandle;
    @Autowired
    private WebSocketInterceptor webSocketInterceptor;

    /**
     * 自动扫描使用注解ServerEndpoint的类并交由Spring管理
     * */
    @Bean
    public ServerEndpointExporter serverEndpointExporter(){
        return new ServerEndpointExporter();
    }

    /**
     * @param registry
     */
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(chatWebSocketHandle,"/chatWebSocket").addInterceptors(webSocketInterceptor);
    }
}
